/**
*      author: Y_Yao
*      created: 2025-05-10 17:16:23
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 1e4 + 7;
const int M = 5e4 + 7;
const int mod = 1e4;
int n,m,s,t,t0;
struct node
{
    int v,next,w;
}E[M];
int idx = 1;
int bin[N];
int dis[N],cnt[N],h[N];

void toposort()
{
    queue<int> q;
    for(int i = 1;i <= n;i++)
    {
        if(bin[i] == 0)
        {
            q.push(i);
            cnt[i] = 1;
        }
    }

    while(!q.empty())
    {
        auto t1 = q.front();
        q.pop();

        for(int i = h[t1];i;i = E[i].next)
        {
            int next = E[i].v;
            int w = E[i].w;
            dis[next] = (dis[next] + dis[t1] + cnt[t1] * w) % mod;
            cnt[next] = (cnt[next] + cnt[t1]) % mod;
            bin[next]--;
            if(bin[next] == 0) q.push(next);
        }
    }
}

void solve()
{
    cin >> n >> m >> s >> t >> t0;
    for(int i = 1;i <= m;i++)
    {
        int u,v,w;
        cin >> u >> v >> w;
        E[idx++].w = w;
        E[idx].v = v;
        E[idx].next = h[u];
        h[u] = idx;
        bin[v]++;
    }

    toposort();

    cout << (dis[t] + (cnt[t] - 1) * t0 % mod) % mod << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}